package com.ruoyi.screeningDate.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.firefighter.domain.FirefighterInfo;
import com.ruoyi.firefighter.service.IFirefighterInfoService;
import com.ruoyi.ifmEnlistIand.service.ISysDeptService;
import com.ruoyi.screeningDate.domain.*;
import com.ruoyi.screeningDate.mapper.MorescreeningdateMapper;
import com.ruoyi.screeningDate.service.MorescreeningdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.remoting.RemoteTimeoutException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

@Service
@Transactional
public class MorescreeningdateServiceImpl implements MorescreeningdateService {
    @Autowired
    private MorescreeningdateMapper morescreeningdateMapper;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IFirefighterInfoService firefighterInfoService;

    @Override
    public List<FanHui> selectAll(Integer id) {
       List<FanHui>  fanHui = morescreeningdateMapper.selectPersonnelCategoryAndRelatedInfo(id);
        return fanHui;
    }

    @Override
    public List<FanHui> selectAllb(Integer id) {
        List<FanHui>  fanHui = morescreeningdateMapper.selectPersonnelCategoryAndRelatedInfob(id);
        return fanHui;
    }

    @Override
    public List<Punishment> selectPunishmentList() {
        return morescreeningdateMapper.selectPunishmentList();
    }

    @Override
    public List<MoreFilterA> selectFirefightersWithConditions(MoreFilterB moreFilterB) {

        ArrayList<MoreFilterA> moreFilterAS1 = new ArrayList<>();
        List<FirefighterInfo> firefighterInfos = GetzUnit(moreFilterB.getLoginDeptId());
        List<MoreFilterA> moreFilterAS = morescreeningdateMapper.selectFirefightersWithConditions(moreFilterB);
        for (FirefighterInfo info : firefighterInfos) {
            for (MoreFilterA moreFilterA : moreFilterAS) {
                if (info.getUserId()!=null&&info.getUserId().toString().equals(moreFilterA.getUserId())) {
                    moreFilterAS1.add(moreFilterA);
                }
            }
        }
        return moreFilterAS1;
    }

    @Override
    public List<Hometown> selectHometownList() {
        return morescreeningdateMapper.selectHometownList();
    }

    @Override
    public List<PerformanceT> selectVoTneng(Performance performance) {
        return morescreeningdateMapper.selectVoTneng( performance);
    }

    @Override
    public List<PerformanceM> selectVoTneng1(Performance performance) {
        return morescreeningdateMapper.selectVoTneng1( performance);
    }
    public List<FirefighterInfo> GetzUnit(String deptId1){
        if(deptId1==null){
            throw new RemoteTimeoutException("值不能为空");
        }
        Long deptId =  Long.parseLong(deptId1);

        List<SysDept> sysDepts= sysDeptService.selectDeptListbyAncestors(deptId);
        HashSet<Integer> result = new HashSet<>();
        for (SysDept sysDept : sysDepts) {
            result.add(sysDept.getDeptId().intValue());
            String ancestors = sysDept.getAncestors();
            String[] parts = ancestors.split(",");
            boolean startFound = false;
            for (String part : parts) {
                if (startFound) {
                    result.add(Integer.parseInt(part));
                } else if (part.equals(deptId.toString())) {
                    startFound = true;
                }
            }
        }
        Integer deptIdInt = deptId != null ? deptId.intValue() : 0;
        result.add(deptIdInt);
        ArrayList<Integer> a1 = new ArrayList<>();
        for (Integer integer : result) {
            a1.add(integer);
        }
        List<SysDept> sysDeptslist=  sysDeptService.selectListbyb(a1);
        FirefighterInfo firefighterInfo = new FirefighterInfo();
        List<FirefighterInfo> list = firefighterInfoService.selectFirefighterInfoList(firefighterInfo);
        ArrayList<FirefighterInfo> moreFilterAS = new ArrayList<>();
        for (SysDept dept : sysDeptslist) {
            for (FirefighterInfo filterA : list) {
                if (dept.getDeptName().equals(filterA.getUnit())) {
                    moreFilterAS.add(filterA);
                }
            }
        }
        return moreFilterAS;
    }
}
